
import pyodbc
import time

import yaml
import os
import sys
import json
yamlPath = os.path.dirname(os.path.realpath(sys.argv[0])) + '\\config.yaml'
f = open(yamlPath,'r',encoding='utf-8')
cont = f.read()
msInfo = yaml.load(cont, Loader=yaml.FullLoader)['msInfo']
f.close()
tablehead=['positionname','deviceAddress','jcsj','zdbh','SO2','NO2','PM10','CO','O3','PM25','Temperature','Humidity','upload_time']
def getdbdata(dbname):
    conn = conn = pyodbc.connect(DRIVER='{SQL Server}',SERVER=msInfo['host'],DATABASE=dbname,UID=msInfo['user'],PWD=msInfo['passwd'])
    cursor =conn.cursor()
    msg =cursor.execute('SET NOCOUNT ON')
    msg =cursor.execute('exec getallrealdata')
    rows=cursor.fetchall()
    cursor.close()
    conn.close()
    jsondata=[]
    for row in rows:
        data={}
        for i in range(0,13):
            data[tablehead[i]]=str(row[i])
        jsondata.append(data)
    print(str(len(jsondata)))
    return  json.dumps(jsondata, ensure_ascii=False)
    # return  jsondata
# getdbdata()
